<!DOCTYPE html>
<html>
<script src="js/jquery-3.1.1.js"></script>
<link href="static/assets/global/plugins/bootstrap/css/bootstrap.min.css" style="stylesheet" type="text/css" />
<link href="static/assets/global/css/components.css" rel="stylesheet" type="text/css" />
<link href="static/assets/admin/pages/css/login.css" rel="stylesheet" type="text/css" />
<head>
	<meta charset="utf-8">
	<title></title>
</head>
<body class="login">
<div class="content">
	<h3 class="form-title">商品详情</h3>

	<div class="form-group" id="promoStartDateContanier">
		<p></p>
		<label style="color: red" class="control-label">秒杀开始时间</label>
		<div>
			<label style="color: red" class="control-label" id="promoStartDate" />
		</div>
	</div>

	<div class="form-group">
		<div>
			<label class="control-label" id="title" />
		</div>
	</div>

	<div class="form-group">
		<p></p>
		<label class="control-label">商品描述</label>
		<div>
			<label class="control-label" id="description" />
		</div>
	</div>

	<div class="form-group" id="priceContanier">
		<p></p>
		<label class="control-label">价格</label>
		<div>
			<label class="control-label" id="price" />
		</div>
	</div>

	<div class="form-group" id="promoPriceContanier">
		<p></p>
		<label style="color: red" class="control-label">秒杀价格</label>
		<div>
			<label style="color: red" class="control-label" id="promoPrice" />
		</div>
	</div>

	<div class="form-group">
		<p></p>
		<div>
			<img class="control-label" id="imgUrl" style="width:200px;height: auto;"/>
		</div>
	</div>

	<div class="form-group">
		<label class="control-label">库存</label>
		<div>
			<label class="control-label" id="stock" />
		</div>
	</div>

	<div class="form-group">
		<label class="control-label">销量</label>
		<div>
			<label class="control-label" id="sales" />
		</div>
	</div>

	<div class="form-actions">
		<button class="btn blue" id="createorder">下单</button>
	</div>
</div>
</body>

<script type="text/javascript">
	function getRequest() {
		var url = location.search; //获取url中"?"符后的字串
		var theRequest = new Object();
		if (url.indexOf("?") != -1) {
			var str = url.substr(1);
			strs = str.split("&");
			for(var i = 0; i < strs.length; i ++) {
				theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
			}
		}
		//console.log(theRequest);
		return theRequest;
	};

	var g_itemVO = {};
	$(document).ready(function() {
		$("#createorder").on("click",function(){
			$.ajax({
				type:"POST",
				url:"/order/createorder",
				//contentType:"application/x-www-form-urlencoded",
				data:{
					"itemId":getRequest("id").id,
					"account":1,
					//"promoId":g_itemVO.promoId
				},
				xhrFields:{withCredentials:true},
				success:function(data){
					if(data.status == "success"){
						alert("下单成功");
						console.log(data);
						//location.replace(location.href);
						//setInterval(reloadDom,1000);
					}else{
						//alert("下单失败，原因为"+data.data.errMsg);
						console.log("错误码" + data.errCode);
						console.log(data);
						if(data.errCode==20003){
							console.log("还未登录");
							window.location.href="login.html";
						}
					}
				},
				error:function(data){
					alert("下单失败，原因为"+data.responseText);
				}
			});
			return false;
		});

		$.ajax({
			type:"GET",
			url:"/item/getItemById",
			data:{
				"id":getRequest("id").id,
			},
			xhrFields:{withCredentials:true},
			success:function(data){
				if(data.status == "success"){
					g_itemVO = data.data;
					console.log(data.data);
					console.log(window.location);
					reloadDom();
					setInterval(reloadDom,1000);
				}else{
					alert("查询失败，原因为"+data.errMsg)
				}
			},
			error:function(data){
				alert("查询失败，原因为"+data.responseText);
			}
		})
	});

	function reloadDom(){
		$("#title").text(g_itemVO.title);
		$("#description").text(g_itemVO.description);
		$("#stock").text(g_itemVO.stock);
		$("#price").text(g_itemVO.price);
		$("#imgUrl").attr("src",g_itemVO.imgUrl);
		$("#sales").text(g_itemVO.price);
		if (g_itemVO.promoStatus == 1){
			var startTime = g_itemVO.startDate.replace(new RegExp("-","gm"),"/");
			startTime = (new Date(startTime)).getTime();
			var nowTime = Date.parse(new Date());
			var delta = (startTime - nowTime)/1000;
			//秒杀活动还未开始，显示开始时间和对应价格
			$("#promoStartDate").text("秒杀活动将于："+g_itemVO.startDate+"开售;倒计时："+delta+"秒");
			$("#promoPrice").text(g_itemVO.promoPrice);
			$("#createorder").attr("disabled",true);
		}else if (g_itemVO.promoStatus == 2){
			//秒杀活动正在进行中
			$("#promoStartDate").text("秒杀正在进行中");
			$("#promoPrice").text(g_itemVO.promoPrice);
			$("#priceContanier").css('display','none');
			$("#createorder").attr("disabled",false);
		}else{
			$("#promoStartDateContanier").css('display','none');
			$("#promoPriceContanier").css('display','none');
		}
	}
</script>
</html>
